/**
 * 
 */
package ca.lscs.dal;

import java.util.List;
import java.util.Map;

import ca.lscs.model.AppUser;
import ca.lscs.model.Checklist;
import ca.lscs.model.ChecklistGroup;
import ca.lscs.model.ChecklistItem;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.helper.DAOBase;

/**
 * @author Steve Pennington
 *
 */
public class ChecklistDao extends DAOBase {
	
	private static ChecklistDao instance = new ChecklistDao();
	
	private ChecklistDao() {
		ObjectifyService.register(AppUser.class);
		ObjectifyService.register(Checklist.class);
		ObjectifyService.register(ChecklistItem.class);
		ObjectifyService.register(ChecklistGroup.class);
	}
	
	public static ChecklistDao getInstance() {
		return instance;
	}
	
	public Checklist getById(long id) {
		return ofy().find(Checklist.class, id);
	}
	
	public List<Checklist> getOpenByCreator(AppUser manager) {
		return ofy().query(Checklist.class).filter("creator", manager).filter("submitted", false).list();
	}
	
	public List<Checklist> getSubmittedByCreator(AppUser manager) {
		return ofy().query(Checklist.class).filter("creator", manager).filter("submitted", true).list();
	}
	
	public List<Checklist> getByCreator(AppUser manager) {
		return ofy().query(Checklist.class).filter("creator", manager).list();
	}
	
	public List<Checklist> getOpenByAsignee(AppUser surveyor) {
		return ofy().query(Checklist.class).filter("asignee", surveyor).filter("submitted", false).list();
	}
	
	public List<Checklist> getSubmittedByAsignee(AppUser surveyor) {
		return ofy().query(Checklist.class).filter("asignee", surveyor).filter("submitted", true).list();
	}
	
	public List<Checklist> getByAsignee(AppUser surveyor) {
		return ofy().query(Checklist.class).filter("asignee", surveyor).list();
	}

	public Key<Checklist> put(Checklist checklist) {
		return ofy().put(checklist);
	}
	
	public void delete(Checklist checklist) {
		ofy().delete(checklist);
	}
	
	public List<ChecklistGroup> getChecklistGroups() {
		return ofy().query(ChecklistGroup.class).order("order").list();
	}
	
	public List<ChecklistItem> getChecklistItems() {
		return ofy().query(ChecklistItem.class).order("order").list();
	}
	
	public List<ChecklistItem> getChecklistItems(ChecklistGroup group) {
		return ofy().query(ChecklistItem.class).filter("group", group).order("order").list();
	}
	
	public Map<Key<ChecklistGroup>, ChecklistGroup> putGroups(List<ChecklistGroup> group) {
		return ofy().put(group);
	}
	
	public Map<Key<ChecklistItem>, ChecklistItem> putItems(List<ChecklistItem> item) {
		return ofy().put(item);
	}
	
}
